/**
 * 
 */
package com.nari.interfaces.service;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.nari.baseinfo.dao.IFileMappingDao;
import com.nari.basic.model.IUser;
import com.nari.coreframe.webframe.SysEnvironmentMgr;
import com.nari.ftpclient.business.IRemoteFileManage;
import com.nari.heatshare.dao.IAssignMeterDataFileDao;
import com.nari.heatshare.model.IAssignMeterDataFile;
import com.nari.heatshare.model.ITemperaturePointDataGather;
import com.nari.heatshare.service.IAssignMeterDataStatisticsService;
import com.nari.heatshare.service.ITemperaturePointDataGatherService;
import com.nari.interfaces.decipher.gprs.vo.UploadMeterDataSeal;
import com.nari.interfaces.decipher.pda.PdaUploadSeal;
import com.nari.platform.date.CalendarConstants;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.WebConstants;


/**
 * 
 * @author Xiaopeng
 * @date 2013-11-13
 */
public class PdaUploadServiceImpl implements IPdaUploadService {
	private String diskFilePath;
	private String meterDataFileFolder;
	private IAssignMeterDataFileDao iAssignMeterDataFileDao;
	private IFileMappingDao iFileMappingDao;
	private IHeatMeterData24GatherService iHeatMeterData24GatherService;
	private IAssignMeterDataStatisticsService iAssignMeterDataStatisticsService;
	private IRemoteFileManage iRemoteFileManage;
	private ITemperaturePointDataGatherService iTemperaturePointDataGatherService;
	/**
	 * 保存掌机上传的分配表数据文件
	 * @param attachFile 数据文件附件
	 * @param attachFileFileName 数据文件名
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-1
	 */
	public ResponseMsg saveDataFileUpload(File attachFile, String attachFileFileName) throws Exception {
		//文件存储目录
		File saveFolder = new File(this.diskFilePath + "/" + this.meterDataFileFolder);
		if(!saveFolder.exists()) {
			saveFolder.mkdirs();
		}
		//解析掌机上传的数据文件
		UploadMeterDataSeal pdaMeterDataSeal = PdaUploadSeal.parsePdaUpload(attachFile.getAbsolutePath());
		if(pdaMeterDataSeal != null) {
			//抄表日期
			Date recordTime = CalendarUtils.parseStringToDate(pdaMeterDataSeal.getRecordTime(), CalendarConstants.dateFormat);
			//掌机数据文件实体
			IAssignMeterDataFile meterDataFile = new IAssignMeterDataFile();
			//设置上传附件名和上传人
			if(attachFileFileName!=null && !"".equals(attachFileFileName)) {
				//人工上传附件
				IUser sysLoginUser = (IUser) SysEnvironmentMgr.getSessionInstance().getAttribute(WebConstants.USER_SESSION);
				meterDataFile.setUploadMan(sysLoginUser.getUserID());
			} else {
				//附件名称重新定义：集中器号_抄表日期-yyyyMMddHHmmss.xml
				String timestamp = CalendarUtils.parseDateToString(new Date(), CalendarConstants.dateTimeFormat_stamp);
				attachFileFileName = pdaMeterDataSeal.getConcentratorNo() 
						+ "_" +pdaMeterDataSeal.getRecordTime()
						+"-"+timestamp + ".xml";
				//上传人
				meterDataFile.setUploadMan("掌机上传"); 
			}
			meterDataFile.setFileName(attachFileFileName);
			//文件大小
			meterDataFile.setFileSize(attachFile.length());
			//上传时间
			meterDataFile.setUploadDate(new Date());
			//文件的存储磁盘
			meterDataFile.setDiskID(this.diskFilePath);
			//文件存储的相对路径
			String ranFileName = CalendarUtils.parseDateToString(new Date(), CalendarConstants.dateTimeMiliPreciseFormat_stamp);
			ranFileName = pdaMeterDataSeal.getConcentratorNo() + "_" +pdaMeterDataSeal.getRecordTime()+"_"+ranFileName;
			String relativeSavePath = this.meterDataFileFolder + "/" + ranFileName + ".xml";
			//文件存储的相对路径
			meterDataFile.setSaveUrl(relativeSavePath);
			String dataFileUID = iAssignMeterDataFileDao.saveEntity(meterDataFile);
			//将附件文件保存到磁盘中
			this.iFileMappingDao.saveUpload(attachFile, new File(this.diskFilePath + "/" +relativeSavePath));
			//读取的分配表记录数
			meterDataFile.setAmRecordNum(pdaMeterDataSeal.getAssignMeterDatas().size());
			//批量新增掌机上传的分配表数据
			ResponseMsg amSaveMsg = this.iAssignMeterDataStatisticsService.saveMeterDataBatch(pdaMeterDataSeal.getAssignMeterDatas(), 
					pdaMeterDataSeal.getConcentratorNo(), recordTime, dataFileUID, 3); //掌机上传数据
			//将温度点放入数据库中
			List<ITemperaturePointDataGather> tempPointDatas = pdaMeterDataSeal.getTempPointDatas();
			if(tempPointDatas!=null&&tempPointDatas.size()>1) {
				for(ITemperaturePointDataGather temp : tempPointDatas)
				this.iTemperaturePointDataGatherService.saveOrUpdateEntity(temp);
			}
			if(amSaveMsg.isSuccess()) {
				//分配表数据上传成功数
				meterDataFile.setAmSuccessNum(new Integer(amSaveMsg.getMsg()));
			}
			//读取热量表记录数
			meterDataFile.setHmRecordNum(pdaMeterDataSeal.getHeatMeterDatas().size());
			//批量新增掌机上传的热量表数据
			int hmSuccessNum = iHeatMeterData24GatherService.saveMeterDataBatch(pdaMeterDataSeal.getHeatMeterDatas(), 
					dataFileUID, 3); ////掌机上传数据
			//热量表数据上传成功数
			meterDataFile.setHmSuccessNum(hmSuccessNum);
			//修正掌机抄表信息
			iAssignMeterDataFileDao.updateEntity(meterDataFile, dataFileUID);
			//将痕迹文件存储到FTP服务器
			iRemoteFileManage.uploadFile("", ranFileName + ".xml", meterDataFile.getDiskID()+"/"+meterDataFile.getSaveUrl());
			return ResponseMsg.getInstance(true, "");
		}
		return ResponseMsg.getInstance(false, "掌机文件无法解析，请重新上传！");
	}
	
	/**
	 * @return the diskFilePath
	 */
	public String getDiskFilePath() {
		return diskFilePath;
	}

	/**
	 * @param diskFilePath the diskFilePath to set
	 */
	public void setDiskFilePath(String diskFilePath) {
		this.diskFilePath = diskFilePath;
	}

	/**
	 * @return the meterDataFileFolder
	 */
	public String getMeterDataFileFolder() {
		return meterDataFileFolder;
	}

	/**
	 * @param meterDataFileFolder the meterDataFileFolder to set
	 */
	public void setMeterDataFileFolder(String meterDataFileFolder) {
		this.meterDataFileFolder = meterDataFileFolder;
	}

	/**
	 * @return the iAssignMeterDataFileDao
	 */
	public IAssignMeterDataFileDao getiAssignMeterDataFileDao() {
		return iAssignMeterDataFileDao;
	}

	/**
	 * @param iAssignMeterDataFileDao the iAssignMeterDataFileDao to set
	 */
	public void setiAssignMeterDataFileDao(
			IAssignMeterDataFileDao iAssignMeterDataFileDao) {
		this.iAssignMeterDataFileDao = iAssignMeterDataFileDao;
	}

	/**
	 * @return the iFileMappingDao
	 */
	public IFileMappingDao getiFileMappingDao() {
		return iFileMappingDao;
	}

	/**
	 * @param iFileMappingDao the iFileMappingDao to set
	 */
	public void setiFileMappingDao(IFileMappingDao iFileMappingDao) {
		this.iFileMappingDao = iFileMappingDao;
	}

	/**
	 * @return the iHeatMeterData24GatherService
	 */
	public IHeatMeterData24GatherService getiHeatMeterData24GatherService() {
		return iHeatMeterData24GatherService;
	}

	/**
	 * @param iHeatMeterData24GatherService the iHeatMeterData24GatherService to set
	 */
	public void setiHeatMeterData24GatherService(
			IHeatMeterData24GatherService iHeatMeterData24GatherService) {
		this.iHeatMeterData24GatherService = iHeatMeterData24GatherService;
	}

	/**
	 * @return the iAssignMeterDataStatisticsService
	 */
	public IAssignMeterDataStatisticsService getiAssignMeterDataStatisticsService() {
		return iAssignMeterDataStatisticsService;
	}

	/**
	 * @param iAssignMeterDataStatisticsService the iAssignMeterDataStatisticsService to set
	 */
	public void setiAssignMeterDataStatisticsService(
			IAssignMeterDataStatisticsService iAssignMeterDataStatisticsService) {
		this.iAssignMeterDataStatisticsService = iAssignMeterDataStatisticsService;
	}

	/**
	 * @return the iRemoteFileManage
	 */
	public IRemoteFileManage getiRemoteFileManage() {
		return iRemoteFileManage;
	}

	/**
	 * @param iRemoteFileManage the iRemoteFileManage to set
	 */
	public void setiRemoteFileManage(IRemoteFileManage iRemoteFileManage) {
		this.iRemoteFileManage = iRemoteFileManage;
	}

	public ITemperaturePointDataGatherService getiTemperaturePointDataGatherService() {
		return iTemperaturePointDataGatherService;
	}

	public void setiTemperaturePointDataGatherService(
			ITemperaturePointDataGatherService iTemperaturePointDataGatherService) {
		this.iTemperaturePointDataGatherService = iTemperaturePointDataGatherService;
	}

}
